<?php
/**
 * 功能：登录验证
 * 日期：2014-8-14
 * 作者：zx
 */
function is_login() {
	$member = session ( 'member' );
	if (empty ( $member )) {
		return false;
	} else {
		return true;
	}
}
/*
 * 上传媒体文件至微信服务器
 */
function upload_file_to_weixin($type,$filepath){
    $access_token=get_accesstoken();
    //echo $access_token;
    $filedata = array("media" => "@".$filepath);
    //dump($filedata);
    $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$access_token."&type=".$type;
    //echo $filedata;
    $result = https_request($url,$filedata);
    return $result;
}
function https_request($url, $data = null)
{
    //dump($data);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    //var_dump(curl_error($curl));
    //dump($output);
    curl_close($curl);
    return $output;
}
/*
 * 获取accesstoken
 */
function get_accesstoken(){
    $appid = $_SESSION['now_pub']["appid"];
    $appsecret = $_SESSION['now_pub']["secret"];
    //echo $appid."   ".$appsecret;
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    //echo $url;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $jsoninfo = json_decode($output, true);
    $access_token = $jsoninfo["access_token"];
    return $access_token;
}
/**
 * 功能：文件删除函数
 * 日期：2014-8-14
 * 作者：zx
 */
function rmdirs($dir){
    //error_reporting(0);    函数会返回一个状态,我用error_reporting(0)屏蔽掉输出
    //rmdir函数会返回一个状态,我用@屏蔽掉输出
    $dir_arr = scandir($dir);
    if(!$dir_arr){
        foreach($dir_arr as $key=>$val){
            if($val == '.' || $val == '..'){}
            else {
                if(is_dir($dir.'/'.$val))
                {
                    if(@rmdir($dir.'/'.$val) == 'true'){}    //去掉@您看看
                    else
                        rmdirs($dir.'/'.$val);
                }
                else
                    unlink($dir.'/'.$val);
            }
        }
    }
}
//循环删除目录和文件函数
function delDirAndFile( $dirName )
{
    if ($handle = opendir("$dirName")) {
        while ( false !== ( $item = readdir( $handle ) ) ) {
            if ( $item != "." && $item != ".." ) {
                if ( is_dir( "$dirName/$item" ) ) {
                    delDirAndFile( "$dirName/$item" );
                } else {
                    if( unlink( "$dirName/$item" ) );
                }
            }
        }
        closedir( $handle );
        if( rmdir( $dirName ) );
    }
}
/**
 * 转换成数组
 * @param unknown $obj
 * @return multitype:NULL unknown
 */
function objtoarr($obj){
    $ret = array();
    foreach($obj as $key =>$value){
        if(gettype($value) == 'array' || gettype($value) == 'object'){
            $ret[$key] = objtoarr($value);
        }
        else{
            $ret[$key] = $value;
        }
    }
    return $ret;
}
function arrtojson($obj){
    $ret = array();
    foreach($obj as $key =>$value){
        if(gettype($value) == 'array' || gettype($value) == 'object'){
            $ret[$key] = arrtojson($value);
        }
        else{
            $ret[$key] = $value;
        }
    }
    return $ret;
}
function arrayRecursive(&$array, $function, $apply_to_keys_also = false){
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }
        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}
function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
}

?>